<?php
use yii\helpers\Url;
use yii\web\View;
// use yii\Web\View;
use app\assets\NewCreativeAsset;
NewCreativeAsset::register($this);
$this->title = $this->context->pageTitle.Yii::$app->params['titleSuffix'];
$this->params['breadcrumbs'] = [
  [
    'label' => \Yii::t('creative', 'Creative'),
    'url' => ['creative-list']
  ],
  \Yii::t('creative', 'Edit Creative')
];
$jsMsgs = $this->context->loadJsMsgs([
  'creativeForm'
]);
$this->registerJs("VAM.setVar('LANGUAGE_JS', '".$jsMsgs."');", View::POS_HEAD);
?>
<div class="page-header">
  <h1>
    <?=\Yii::t('creative', 'Creative')?>
    <small><?=\Yii::t('creative', 'Edit Mobile Video Creative')?></small>
  </h1>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <i class="fa fa-edit"></i>
    <?=\Yii::t('creative', 'Edit Mobile Video Creative')?>
    <ul class="list-unstyled pull-right">
      <li>
        <a href="#">
          <i class="fa fa-chevron-up"></i>
        </a>
      </li>
    </ul>
  </div>
  <div class="panel-body">
    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <form class="form-horizontal" id="data-frm" method="POST" action="<?php echo Url::to(['creative/new-mobile-video-creative-handle']);?>">
          <input class="hidden" type="text" name="id" value="<?=$creative['id']?>">
          <fieldset>
            <legend><?=\Yii::t('creative', 'Creative Info')?></legend>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="creative_name" >
                <em>*</em>
                <?=\Yii::t('creative', 'Creative Name')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="creative_name" type="text" name="name" value="<?=$creative['name']?>" placeholder="<?=\Yii::t('creative', 'Creative Name')?>">
                <div class="help-block text-error" id="creative_name_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="creative_vocation_large" >
                <em>*</em>
                <?=\Yii::t('creative', 'Vocation')?>：
              </label>
              <div class="col-sm-3">
                <select class="form-control" id="creative_vocation_large" name="parent_category">
                </select>
              </div>
              <div class="col-md-3">
                <select class="form-control" id="creative_vocation_small" name="category">
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="advertiser_name" >
                <?=\Yii::t('creative', 'Advertiser')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="advertiser_name" type="text" name="advertiser_name" value="<?=$creative['advertiser_name']?>" placeholder="<?=\Yii::t('creative', 'Advertiser')?>">
                <div class="help-block text-error" id="advertiser_name_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="landing_page_url">
                <em>*</em>
                <?=\Yii::t('creative', 'Target Url')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="landing_page_url" type="text" name="landing_page" value="<?=$creative['landing_page']?>" placeholder="http(s)://">
                <div class="help-block text-error" id="landing_page_url_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="landing_page_main_url">
                <?=\Yii::t('creative', 'Adomain')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="landing_page_main_url" type="text" name="adomain_list" value="<?= $creative['adomain_list'] ?>" placeholder="<?=\Yii::t('creative', 'Please input primary domain or secondary domain.')?>">
                <div class="help-block text-error" id="landing_page_main_url_err"></div>
                <p class="help-block"><?=\Yii::t('creative', 'Hint: Input baidu.com for http://www.baidu.com and input sina.com.cn for http://www.sina.com.cn, likewise.')?></p>
              </div>
            </div>
      <?php
        $shows = explode(';;;', $creative['show_monitor_url']);
        $title = \Yii::t('creative', 'Click to add show url');
        $showUrl = \Yii::t('creative', 'Show Url').':';
        foreach ($shows as $i => $show) {
          $btn = ' del-operate';
          $icon = 'fa-minus-circle';
          $label = '';
          $i == 0 && $btn = 'add-monitor-url';
          $i == 0 && $icon = 'fa-plus-circle';
          $i == 0 && $label = $showUrl;

          echo $showDiv = <<<FINE
            <div class="form-group impr-monitor-url monitor-url-form">
              <label class="col-sm-3 control-label" for="impr_monitor_url">
                $label
              </label>
              <div class="col-sm-7">
                <input class="form-control" type="text" data-role="show_url" name="show_monitor_url[]" value="$show" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a href="javascript:void(0);" class="btn btn-link $btn" title="$title" data-type="impr-monitor-url">
                <i class="fa $icon"></i>
              </a>
            </div>
FINE;
        }
      ?>
            <div class="form-group impr-monitor-url-new impr-monitor-url monitor-url-form hidden">
              <label class="col-sm-3 control-label" for="impr_monitor_url">
                <!-- <?=\Yii::t('creative', 'Show Url')?>： -->
              </label>
              <div class="col-sm-7">
                <input class="form-control" type="text"  data-role="show_url" name="show_monitor_url[]" value="" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a href="javascript:void(0);" class="btn btn-link del-operate" title="<?=\Yii::t('creative', 'Click to delete show url')?>" data-type="impr-monitor-url">
                <i class="fa fa-plus-circle fa-minus-circle"></i>
              </a>
            </div>
      <?php
        $clicks = explode(';;;', $creative['click_monitor_url']);
        $title = \Yii::t('creative', 'Click to add click url');
        $showUrl = \Yii::t('creative', 'Click Url').':';
        foreach ($clicks as $i => $click) {
          $btn = ' del-operate';
          $icon = 'fa-minus-circle';
          $label = '';
          $i == 0 && $btn = 'add-monitor-url';
          $i == 0 && $icon = 'fa-plus-circle';
          $i == 0 && $label = $showUrl;

          echo $showDiv = <<<FINE
            <div class="form-group click-monitor-url monitor-url-form">
              <label class="col-sm-3 control-label" for="click_monitor_url" >
                $label
              </label>
              <div class="col-sm-7">
                <input class="form-control" data-role="click_url" type="text" name="click_monitor_url[]" value="$click" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a href="javascript:void(0);" class="btn btn-link $btn" title="$title" data-type="click-monitor-url">
                <i class="fa $icon"></i>
              </a>
            </div>
FINE;
        }
      ?>
            <div class="form-group click-monitor-url-new click-monitor-url monitor-url-form hidden">
              <label class="col-sm-3 control-label" for="click_monitor_url" >
                <!-- <?=\Yii::t('creative', 'Click Url')?>： -->
              </label>
              <div class="col-sm-7">
                <input class="form-control" data-role="click_url" type="text" name="click_monitor_url[]" value="" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a href="javascript:void(0);" class="btn btn-link del-operate" title="<?=\Yii::t('creative', 'Click to delete click url')?>" data-type="click-monitor-url">
                <i class="fa fa-plus-circle fa-minus-circle"></i>
              </a>
            </div>      
            <div class="form-group hidden">
              <label for="belong_creative" class="col-sm-3 control-label">
                <?=\Yii::t('creative', 'Template')?>：
              </label>
              <div class="col-sm-7">
                <select class="form-control" id="belong_creative" name="template_id" placeholder="<?=\Yii::t('creative', 'Template')?>">
        		      <?php 
                    foreach($templates as $template) {
                      $id = $template['id'];
                      $name = $template['name'];
                      echo "<option value='$id'>$name</option>";
                    }
                  ?>
		            </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Valid Size')?>：
              </label>
              <div class="col-sm-7">
                <p class="form-control-static" id="alternative_creative_size" name="alternative_creative_size">
                  <?php 
                    $sizeList = [];
                    foreach ($sizes as $k => $size) {
                      $sizeList[] = $size['width'].'*'.$size['height'];
                    }
                    $sizeList = array_unique($sizeList);
                    sort($sizeList);
                    foreach ($sizeList as $k => $size) {
                      echo $size.',  ';
                      if(($k+1)%9 == 0) echo '</br>';
                    }
                  ?>
                </p>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Valid Material Type')?>：
              </label>
              <div class="col-sm-7">
                <p class="form-control-static" id="alternative_creative_size" name="alternative_creative_size">FLV,MP4,OGG,WebM</p>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="pic_url">
                <?=\Yii::t('creative', 'Cover URL')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="pic_url" type="text" name="pic_url" value="<?=$creative['pic_url']?>" placeholder="<?=\Yii::t('creative', 'http(s)://')?>">
                
                <div class="help-block text-error" id="pic_url_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="title">
                <?=\Yii::t('creative', 'Title')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="title" type="text" name="title" value="<?=$creative['title']?>" placeholder="<?=\Yii::t('creative', 'title')?>">
                
                <div class="help-block text-error" id="title_err"></div>
              </div>
            </div>
            <div class="form-group" id="material_source">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Material Source')?>：
              </label>
              <div class="col-sm-7">
                <label class="radio-inline">
                  <input type="radio" name="material_source" value="0" <?if($creative['material_source'] == 0) echo "checked";?>><?=\Yii::t('creative', 'Upload Material')?>
                </label>
                <label class="radio-inline">
                  <input type="radio" name="material_source" value="1" <?if($creative['material_source'] == 1) echo "checked";?>><?=\Yii::t('creative', 'Custom Url')?>
                </label>
              </div>
            </div>
            <div id="custom_material_url">
              <div class="form-group">
                <label for="custom-url" class="control-label col-sm-3">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Material Url')?>：
                </label>
                <div class="col-sm-7">
                  <input type="text" id="custom_url" class="form-control" name="custom_url" value="<?if($creative['material_source'] == 1) echo $creative['file_url'];?>" data-err="custom_url_err">
                  <div class="help-block text-error" id="custom_url_err"></div>
                </div>
              </div>
              <div class="form-group" id="custom_adsize">
                <label class="control-label col-md-3">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Material Size')?>：
                </label>
                <label class="control-label col-md-1 pr0">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Width')?>：
                </label>
                <div class="col-md-1 plr0">
                  <input class="form-control" type="text" id="adspace_width" name="width" value="<?if($creative['material_source'] == 1) echo $creative['width'];?>" data-err="width_err">
                  <div class="help-block text-error" id="width_err"></div>
                </div>
                <label class="control-label col-md-1 pr0">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Height')?>：
                </label>
                <div class="col-md-1 plr0">
                  <input class="form-control" type="text" id="adspace_height" name="height"  value="<?if($creative['material_source'] == 1) echo $creative['height'];?>" data-err="height_err">
                  <div class="help-block text-error" id="height_err"></div>
                </div>
                <label class="control-label col-md-1 plr0">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Duration')?>：
                </label>
                <div class="col-md-1 plr0">
                  <input class="form-control" type="text" id="adspace_length" name="duration" value="<?if($creative['material_source'] == 1) echo $creative['duration'];?>" data-err="length_err">
                  <div class="help-block text-error" id="length_err"></div>
                </div>
              </div>
              <div class="form-group">
                <label for="material-type" class="col-md-3 control-label">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Material Type')?>：
                </label>
                <div class="col-md-7">
                  <select name="material_type" id="" class="form-control">
                    <option value="FLV" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'FLV') echo "selected";?>>FLV</option>
                    <option value="MP4" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'MP4') echo "selected";?>>MP4</option>
                    <option value="OGG" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'OGG') echo "selected";?>>OGG</option>
                    <option value="WebM" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'WEBM') echo "selected";?>>WebM</option>
                  </select>
                </div>
              </div>
            </div>
            <div id="local_upload_material">
              <div class="form-group">
                <label class="col-sm-3 control-label" for="upload_material">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Upload Material')?>：
                </label>
                <div class="col-sm-2 col-sm-offset-7">
                  <button class="btn btn-info" id="upload-material" type="button">
                    <i class="fa fa-upload"></i>
                    <?=\Yii::t('creative', 'Upload')?>
                  </button>
                </div>
                <div class="col-sm-12 mt10 text-center">
                  <div class="creative-preview-wrap">
                  </div>
                  <img class="img-thumbnail creative_material_default" id="creative_material_default" data-src="holder.js/310x260/text:<?=\Yii::t('creative', 'Not upload')?>" alt="<?=\Yii::t('creative', 'Not upload')?>">
                  <input id="upload_material" name="upload_material" type="hidden" value="<?if ($creative['material_source'] == 0) echo $material['id']?>">
                  <input type="hidden" class="creative-id" value="<?=$creative['id']?>">
                  <input type="hidden" class="creative-width" value="<?if ($creative['material_source'] == 0) echo $material['width']?>" >
                  <input type="hidden" class="creative-height" value="<?if ($creative['material_source'] == 0) echo $material['height']?>" >
                  <?php
                    if ($creative['material_source'] == 0) {
                      switch ($creative['format_id']) {
                        case 3:
                            $type = 'swf';
                            break;
                        case 5:
                            $type = 'flv';
                            break;
                        case 6:
                            $type = 'mp4';
                            break;
                        default:
                            $type = 'flv';
                            break;
                      }
                      $preview = <<<FINE
                      <input type="hidden" class="creative-type" value="$type" >
FINE;
                      echo $preview;
                    }
                  ?>
                  <input type="hidden" class="creative-url" name="url" value="<?if ($creative['material_source'] == 0) echo $videoUrl?>">
                  <div id="creative-preview-details"></div>
                  <div class="help-block text-error" id="upload_material_err"></div>
                </div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <div class="form-group">
              <div class="col-sm-offset-3 col-sm-7">
                <div class="btn-form-group pull-right">
                  <input class="operate-type" type="hidden" name="operate_type" value="save">
                  <button type="submit" class="btn btn-primary" data-loading-text="<i class='fa fa-spinner fa-spin'></i> <?=\Yii::t('common', 'Saving')?>..."><?=\Yii::t('common', 'Save')?></button>
                  <a href="<?php echo Url::to(['creative/creative-list']); ?>" class="btn btn-default"><?=\Yii::t('common', 'Back')?></a>
                </div>
              </div>
            </div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" id="upload-material-modal">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title"><?=\Yii::t('creative', 'Upload Material')?></h4>
      </div>
      <div class="modal-body">
        <div class="portlet-content">
          <span class="btn btn-success btn-sm fileinput-button">
            <i class="fa fa-plus"></i>
            <span><?=\Yii::t('creative', 'File')?></span>
            <input id="creative-id" type="hidden" name="creative_id" value="${id}" >
            <!-- The file input field used as target for the file upload widget -->
            <input type="file" multiple="" name="files[]" id="fileupload">
          </span>
          <br>
          <br>
          <!-- The global progress bar -->
          <div class="progress" id="progress">
              <div aria-valuemax="100" aria-valuemin="0" class="progress-bar progress-bar-success progress-bar-striped">0%</div>
          </div>
          <!-- The container for the uploaded files -->
          <div class="files" id="files">
            <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th>ID</th>
                  <th><?=\Yii::t('creative', 'File Name')?></th>
                  <th><?=\Yii::t('creative', 'File Size')?>(KB)</th>
                  <th><?=\Yii::t('creative', 'Upload Time')?></th>
                  <th><?=\Yii::t('creative', 'Info')?></th>
                  <th><?=\Yii::t('creative', 'Uploader')?></th>
                </tr>
              </thead>
              <tbody></tbody>
              </table>
            </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal"><?=\Yii::t('common', 'Close')?></button>
        <button type="button" class="btn btn-primary" id="upload-confirm-operate" data-material-id=""><?=\Yii::t('common', 'Sure')?></button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<?php
  $uploadMaterialUrl = Url::to(['creative/upload-video']);
  $videoPluginUrl = Yii::getAlias('@web/').Yii::$app->params['envDir'].'/js/vendor/flowplayer/flowplayer.swf';
  $creativeVocation = $categories;
  //$creativeVocation = '{"汽车":["车讯行情","车辆养护","配件改装","其它"],"新闻":["军事","时政要闻","产经","科技","其它"],"财经":["股票","基金","理财","房产","保险","收藏","其它"],"数码":["电脑","手机","数码设备","软件","APP","其它"],"医疗":["健康养生","疾病治疗","整形美容","两性","其它"],"娱乐影音":["影视","音乐","图书","图片","星座","明星","游戏","宠物","休闲活动","其它"],"社交与网络服务":["婚恋交友","SNS","论坛","网络服务"],"教育":["幼教","应试","考研","培训","MBA","出国","自考","其它"],"家居家电":["家电","家具","装修","装饰","日常用品"],"体育":["足球","篮球","网球","户外","健身","器材","其它"],"旅行":["酒店和住宿","旅行社和服务","票务服务"],"女性":["时尚","美容","母婴"],"职业":["简历","招聘信息","其它"],"食品饮料":["美食","零食","饮料","烟酒","保健食品","其它"],"服饰鞋包":["服装","鞋靴","配饰","珠宝首饰","箱包","钟表","奢侈品","眼镜"],"工农业":["电子电工","机械设备","农林牧渔","化工及材料"],"生活和商务服务":["生活服务","商务服务"]}';
  $this->registerJs("VAM.setVar('UPLOAD_MATERIAL_URL','$uploadMaterialUrl');", View::POS_END);
  $this->registerJs("VAM.setVar('VIDEO_PLUGIN_URL','$videoPluginUrl');", View::POS_END);
  $this->registerJs("VAM.setVar('CREATIVE_VOCATION','$creativeVocation')", View::POS_END);

  $parentCategory = array_shift($category);
  $childCategory = array_shift($category);
  $this->registerJs("VAM.setVar('parent_category','$parentCategory')", View::POS_END);
  $this->registerJs("VAM.setVar('child_category','$childCategory')", View::POS_END);
?>
<script type="text/javascript" >
  requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/site.js"], function(common) {
    requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/creative/newMobileVideoCreative.js"]);
  });
</script>
